package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO getturnoverStatistics(LocalDate begin, LocalDate end) {

        //创建日期列表
        List<LocalDate> dateList = getdateList(begin, end);

        //创建营业额列表
        List<Double> turnoverList = new ArrayList<>();
        //获取每天的营业额
        for (LocalDate date : dateList) {
            //转换时间类型
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //selete  sum(amount) from orders where order_time >= ? and order_time < ? and status = 5
            Double turnover = orderMapper.sumByTime(beginTime, endTime, Orders.COMPLETED);
            //营业额为null，则设置为0.0
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
    }

    /**
     * 用户数量统计
     * @param begin
     * @param end
     * @return
     */
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //创建日期列表
        List<LocalDate> dateList = getdateList(begin, end);

        //每日新用户数量列表
        List<Integer> newUserList = new ArrayList<>();
        //每日总用户数量列表
        List<Integer> totalUserList = new ArrayList<>();

        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //总的   selete count(id) from user where create_time < ?
            Integer count1 = userMapper.getByTime(null,endTime);
            //新的   selete count(id) from user where create_time >= ? and create_time < ?
            Integer count2 = userMapper.getByTime(beginTime,endTime);

            totalUserList.add(count1);
            newUserList.add(count2);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .build();
    }

    /**
     * 订单数量统计
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        //创建日期列表
        List<LocalDate> dateList = getdateList(begin, end);

        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            //总订单         selete count(id) from orders where order_time >= ? and order_time < ?
            Integer count1 = orderMapper.getByTime(beginTime, endTime, null);
            //有效订单       select count(id) from orders where order_time >= ? and order_time < ? and status = 5
            Integer count2 = orderMapper.getByTime(beginTime, endTime, Orders.COMPLETED);

            totalOrderCount += count1;
            validOrderCount += count2;

            orderCountList.add(count1);
            validOrderCountList.add(count2);
        }
        Double orderCompletionRate =  validOrderCount / totalOrderCount.doubleValue();

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 获取指定时间区间内的日期
     * @param begin
     * @param end
     * @return
     */
    private List<LocalDate> getdateList(LocalDate begin, LocalDate end){
        //创建日期列表
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }
}
